<!DOCTYPE html>
<html style="width:100%;height:100%;">
<head lang="en">
    <meta charset="UTF-8">
    <title>微信默认应答列表</title>
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/default/easyui.css">
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/color.css">
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/icon.css">
    <link type="text/css" rel="stylesheet" href="/resources/css/yidong.css">
    <script type="text/javascript" src="/resources/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/resources/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/resources/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/resources/easyui/plugin/jquery.cookie.js"></script>
    <script type="text/javascript" src="/resources/js/Util.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $('#wechatRespList').datagrid({
                rowStyler:function(index,row){
                    if(row && $.cookie("appId") != row.appId)
                    return 'color:gray';
                }
            });

            var pageSize = $.cookie("pageSize");
            $.cookie("pageNo", 1, {expires:1});
            if(!pageSize) {
                $.cookie("pageSize", 10, {expires:1});
            }
            loadWechatRespList($.cookie("pageSize"), $.cookie("pageNo"));
            //分页操作
            var pager = $('#wechatRespList').datagrid('getPager');
            if(pager){
                $(pager).pagination({
                    onSelectPage:function(pageNumber, pageSize){
                        $.cookie("pageNo", pageNumber, {expires:1});
                        loadWechatRespList(pageSize, pageNumber);
                    },
                    onRefresh:function(pageNumber, pageSize){
                        loadWechatRespList(pageSize, pageNumber);
                    },
                    onChangePageSize:function(pageSize){
                        $.cookie("pageSize", pageSize, {expires:1});
                        loadWechatRespList(pageSize, 1);
                    }
                })
            }
        });

        function searchList() {
            loadWechatRespList($.cookie("pageSize"), 1, null, $('#searchForm').serialize())
        }

        function formatPriority(value,row,index) {
            if(row.priority != null) {
                if(row.priority == 0) {
                    return "中";
                } else if(row.priority == 1) {
                    return "高";
                } else if(row.priority == -1) {
                    return "低";
                }
            }
        }

        function loadWechatRespList(pageSize, pageNo, successFun, condition) {
            var data = 'pageSize='+pageSize+'&pageNo='+pageNo+'&keyvalue=default';
            if(condition) {
                data = data + '&' + condition;
            }
            restClient('/rest/wechat/resp/list', function(data){
                $('#wechatRespList').datagrid("loadData", data);
                if(successFun) successFun();
            }, data);
        }

        function formatPicUrl(value,row,index){
            if(row.picUrl != null) {
                if(row.picUrl){
                    return "<img width='50px' height='50px' src='"+ row.picUrl + "'/>";
                } else {
                    return row.picUrl;
                }
            }
        }
    </script>
</head>
<body onload="isLogon();" style="width:100%;height:100%;margin: 0;">
<table id="wechatRespList" class="easyui-datagrid" style="width:100%;height:100%;"
       toolbar="#toolbar" pagination="true"
       data-options="pageList:[10,15,20],pageSize:$.cookie('pageSize') ? $.cookie('pageSize') : 10"
       rownumbers="true" fitColumns="true" singleSelect="true">
    <thead>
    <tr>
        <th field="msgType" width="30" data-options="formatter:formatMsgType">消息类型</th>
        <th field="content" width="100">文本内容</th>
        <th field="title" width="50">图文标题</th>
        <th field="keyword" width="100">关键字</th>
        <th field="keyvalue" width="50">KEY</th>
        <th field="priority" width="20" data-options="formatter:formatPriority">优先级</th>
        <th field="appId" width="30">APPID</th>
        <th field="status" width="20" data-options="formatter:formatStatus">状态</th>
        <!--<th field="id" width="20">查看</th>-->
    </tr>
    </thead>
</table>
<div id="toolbar">
    <div>
        <a href="javascript:void(0)" class="easyui-menubutton" data-options="menu:'#menuNew',iconCls:'icon-add',plain:'true'">新建</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editOne()">编辑</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyOne()">删除</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-large-picture" plain="true" onclick="setNews()">设置图文</a>
    </div>
    <!--<div style="margin: 5px;">-->
        <!--<form id="searchForm">-->
            <!--文本内容: <input class="easyui-textbox" name="content">-->
            <!--关键字: <input class="easyui-textbox" name="keyword">-->
            <!--KEY: <input class="easyui-textbox" name="keyvalue">-->
            <!--<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="searchList()">Search</a>-->
        <!--</form>-->
    <!--</div>-->
</div>
<div id="menuNew" style="width:100px;">
    <div onclick="newText()">文本消息</div>
    <div onclick="newNews()">图文消息</div>
</div>

<div id="dlg-text" class="easyui-dialog" style="width:400px;height:auto;padding:-20px" data-options="closed:true,
     buttons:[{
        text:'保存',
        handler:function(){
            saveOne($('#dlg-text'), $('#fm-text'));
        }
     },{
        text:'取消',
        handler:function(){
            $('#dlg-text').dialog('close');
        }
     }]">
    <form id="fm-text" method="post">
        <div class="fitem" style="display: none;">
            <input id="text-msgType" name="msgType" class="easyui-textbox"/>
        </div>
        <div class="fitem">
            <label>文本内容：</label>
            <input id="text-content" name="content" class="easyui-textbox easyui-validatebox" style="width: 220px;height: 80px;"
                   required="true" data-options="missingMessage:'该输入项为必输项',multiline:true">
        </div>
        <!--<div class="fitem">-->
            <!--<label>关键字：</label>-->
            <!--<input id="text-keyword" name="keyword" class="easyui-textbox easyui-validatebox"-->
                   <!--required="true" data-options="missingMessage:'该输入项为必输项'">-->
        <!--</div>-->
        <div class="fitem">
            <label>追加菜单：</label>
            <input style="width: 20px;" type="checkbox" id="isMenu" name="isMenu"/>
        </div>
        <!--<div class="fitem">-->
            <!--<label>KEY：</label>-->
            <!--<input id="text-keyvalue" name="keyvalue" class="easyui-textbox easyui-validatebox"-->
                   <!--required="true" data-options="missingMessage:'该输入项为必输项'">-->
        <!--</div>-->
        <!--<div class="fitem">-->
            <!--<label>优先级：</label>-->
            <!--<select id="text-priority" name="priority" class="easyui-combobox easyui-validatebox" data-options="editable:false" required="true">-->
                <!--<option value="1">高</option>-->
                <!--<option value="0">中</option>-->
                <!--<option value="-1">低</option>-->
            <!--</select>-->
        <!--</div>-->
        <div class="fitem">
            <label>APPID：</label>
            <input id="text-appId" name="appId" class="easyui-validatebox easyui-combobox"
                   data-options="valueField:'appId', textField:'appId', formatter:formatAppId, editable:false" required="true">
        </div>
        <div class="fitem">
            <label>状态：</label>
            <select id="text-status" name="status" class="easyui-combobox easyui-validatebox" data-options="editable:false" required="true">
                <option value="0">启用</option>
                <option value="-1">禁用</option>
                <option value="-2">删除</option>
            </select>
        </div>
    </form>
</div>

<div id="dlg-article" class="easyui-dialog" style="width:400px;height:auto;padding:-20px"
     data-options="closed:true,
     buttons:[{
        text:'保存',
        handler:function(){
            saveOne($('#dlg-article'), $('#fm-article'));
        }
     },{
        text:'取消',
        handler:function(){
            $('#dlg-article').dialog('close');
        }
     }]">
    <form id="fm-article" method="post">
        <div class="fitem" style="display: none;">
            <input id="article-msgType" name="msgType" class="easyui-textbox"/>
        </div>
        <div class="fitem">
            <label>图文标题：</label>
            <input id="article-title" name="title" class="easyui-textbox easyui-validatebox" style="width: 220px;height: 80px;"
                   required="true" data-options="missingMessage:'该输入项为必输项',multiline:true">
        </div>
        <!--<div class="fitem">-->
            <!--<label>关键字：</label>-->
            <!--<input id="article-keyword" name="keyword" class="easyui-textbox easyui-validatebox"-->
                   <!--required="true" data-options="missingMessage:'该输入项为必输项'">-->
        <!--</div>-->
        <!--<div class="fitem">-->
            <!--<label>KEY：</label>-->
            <!--<input id="article-keyvalue" name="keyvalue" class="easyui-textbox easyui-validatebox"-->
                   <!--required="true" data-options="missingMessage:'该输入项为必输项'">-->
        <!--</div>-->
        <!--<div class="fitem">-->
            <!--<label>优先级：</label>-->
            <!--<select id="article-priority" name="priority" class="easyui-combobox easyui-validatebox" data-options="editable:false" required="true">-->
                <!--<option value="1">高</option>-->
                <!--<option value="0">中</option>-->
                <!--<option value="-1">低</option>-->
            <!--</select>-->
        <!--</div>-->
        <div class="fitem">
            <label>APPID：</label>
            <input id="article-appId" name="appId" class="easyui-validatebox easyui-combobox"
                   data-options="valueField:'appId', textField:'appId', formatter:formatAppId, editable:false" required="true">
        </div>
        <div class="fitem">
            <label>状态：</label>
            <select id="article-status" name="status" class="easyui-combobox easyui-validatebox" data-options="editable:false" required="true">
                <option value="0">启用</option>
                <option value="-1">禁用</option>
                <option value="-2">删除</option>
            </select>
        </div>
    </form>
</div>

<div id="dlg-setNews" class="easyui-dialog" style="width:500px;height:350px;padding:-20px"
     closed="true" buttons="#dlg-setNews-buttons">
    <table id="tt-setNews" class="easyui-treegrid" style="width:100%;height:100%;"
           data-options="idField:'id', treeField:'text'"
           fitColumns="true" singleSelect="true">
        <thead>
        <tr>
            <th data-options="field:'text',width:100">标题</th>
            <th data-options="field:'description',width:150">描述</th>
            <th data-options="field:'picUrl',width:50, formatter:formatPicUrl">图片</th>
            <th data-options="field:'url',width:100">网址链接</th>
        </tr>
        </thead>
    </table>
</div>
<div id="dlg-setNews-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveSetNews()" style="width:90px">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg-setNews').dialog('close')" style="width:90px">取消</a>
</div>

<script type="text/javascript">
    var url;
    var successMsg;
    function newText(){
        $('#dlg-text').dialog('open').dialog('setTitle','新建文本消息');
        $('#fm-text').form('clear');
        $('#text-msgType').textbox('setValue', 'text');
        loadAppIdCombobox($('#text-appId'), true);
        $('#text-status').combobox('setValue', 0);
        $('#text-priority').combobox('setValue', 0);
        url = '/rest/wechat/resp/create';
        successMsg = "添加成功";
    }
    function newNews(){
        $('#dlg-article').dialog('open').dialog('setTitle','新建图文消息');
        $('#fm-article').form('clear');
        $('#article-msgType').textbox('setValue', 'news');
        loadAppIdCombobox($('#article-appId'), true);
        $('#article-status').combobox('setValue', 0);
        $('#article-priority').combobox('setValue', 0);
        url = '/rest/wechat/resp/create';
        successMsg = "添加成功";
    }
    function saveOne(dlg, fm){
        restClient(url, function (result) {
            if (result.success) {
                dlg.dialog('close');
                loadWechatRespList($.cookie("pageSize"), $.cookie("pageNo"), function(){
                    $.messager.show({
                        title: '温馨提醒',
                        msg: successMsg
                    });
                });
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: result.msg ? result.msg : result.message
                });
            }
        }, fm.serialize()+'&keyvalue=default&priority=-1&keyword=default');
    }
    function editOne(){
        var row = $('#wechatRespList').datagrid('getSelected');
        if (row){
            if($.cookie("appId") == 'woshimorendeappid123' || $.cookie("appId") == row.appId) {
                if (row.msgType == "text") {
                    $('#dlg-text').dialog('open').dialog('setTitle', '编辑文本消息');
                    loadAppIdCombobox($('#text-appId'));
                    $('#fm-text').form('load', row);
                    if(row.isMenu == 1) {
                        document.getElementById('isMenu').checked = true;
                    }
                } else if (row.msgType == "news") {
                    $('#dlg-article').dialog('open').dialog('setTitle', '编辑图文消息');
                    loadAppIdCombobox($('#article-appId'));
                    $('#fm-article').form('load', row);
                }
                url = '/rest/wechat/resp/update/' + row.id;
                successMsg = "编辑成功";
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: '不能编辑默认信息'
                });
            }
        }
    }
    function destroyOne(){
        var row = $('#wechatRespList').datagrid('getSelected');
        if (row){
            if($.cookie("appId") == 'woshimorendeappid123' || $.cookie("appId") == row.appId) {
                $.messager.confirm('确认', '确认要删除么?', function (r) {
                    if (r) {
                        restClient('/rest/wechat/resp/delete/' + row.id, function (result) {
                            if (result.success) {
                                loadWechatRespList($.cookie("pageSize"), $.cookie("pageNo"), function () {
                                    $.messager.show({
                                        title: '温馨提醒',
                                        msg: "删除成功"
                                    });
                                });
                            } else {
                                $.messager.show({
                                    title: '温馨提醒',
                                    msg: result.msg ? result.msg : result.message
                                });
                            }
                        });
                    }
                });
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: '不能删除默认信息'
                });
            }
        }
    }

    var respId;
    function setNews(){
        var row = $('#wechatRespList').datagrid('getSelected');
        if(row) {
            if(row.msgType == "news") {
                respId = row.id;
                $('#dlg-setNews').dialog('open').dialog('setTitle','选择一条图文');
                restClient('/rest/wechat/article/list', function(data) {
                    var setNews = $('#tt-setNews');
                    setNews.treegrid("loadData", data);
                    setNews.treegrid('unselectAll');
                    restClient('/rest/respArticle/list/'+respId, function(result){
                        if(result.success) {
                            var respArticleData = result.data;
                            for (var index in respArticleData) {
                                setNews.treegrid('select', respArticleData[index].articleId);
                            }
                        } else {
                            $.messager.show({
                                title: '温馨提醒',
                                msg: result.msg ? result.msg : result.message
                            });
                        }
                    })
                });
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: '请选择图文消息进行设置'
                });
            }
        }
    }

    function saveSetNews(){
        var node = $('#tt-setNews').treegrid('getSelected');
        var articleId = "";
        if(node) {
            articleId = node.id;
        }
        restClient('rest/respArticle/insert/'+respId, function(result){
            $('#dlg-setNews').dialog('close');
            if(result.success){
                $.messager.show({
                    title: '温馨提醒',
                    msg: '设置成功'
                });
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: result.msg ? result.msg : result.message
                });
            }
        }, {articleId:articleId});
    }
</script>
</body>
</html>